// Copyright 2022 BaihaiAI, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

fn main() {
    #[cfg(target_os = "macos")]
    check_macos_python_is_python3();
    println!("cargo:rustc-env=VERSION={}", git_version::version());
    // if crates dir change, run git version to fetch commit hash again
    println!(
        "cargo:rerun-if-changed={}",
        concat!(env!("CARGO_MANIFEST_DIR"), "/../../")
    );
}

#[cfg(target_os = "macos")]
fn check_macos_python_is_python3() {
    fn get_command_output(command: &str, arg: &str) -> String {
        let output = std::process::Command::new(command)
            .arg(arg)
            .output()
            .unwrap();
        assert!(output.status.success(), "{command} {arg}");
        String::from_utf8(output.stdout).unwrap()
    }
    if get_command_output("python", "--version") != get_command_output("python3", "--version") {
        panic!("python --version and python3 --version is different! can't run in macos");
    }
}
